<template>
  <div class="set-password-container">
    <div class="set-password-card">
      <div class="header">
        <h2>设置账户密码</h2>
        <p class="tip">为了账户安全，请设置您的登录密码</p>
      </div>
      
      <el-form :model="form" :rules="rules" ref="passwordForm" label-width="100px">
        <el-form-item label="新密码" prop="password">
          <el-input 
            type="password" 
            v-model="form.password" 
            placeholder="请输入新密码"
            show-password
            autocomplete="new-password"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input 
            type="password" 
            v-model="form.confirmPassword" 
            placeholder="请再次输入新密码"
            show-password
            autocomplete="new-password"
          ></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="submit-btn" :loading="loading" @click="submitForm">
            确认设置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const router = useRouter()
const passwordForm = ref(null)
const loading = ref(false)

// 表单数据
const form = reactive({
  password: '',
  confirmPassword: ''
})

// 表单验证规则
const rules = {
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ]
}

// 检查用户状态
onMounted(() => {
  const token = localStorage.getItem('token')
  if (!token) {
    ElMessage.warning('请先登录')
    router.push('/login')
  }
})

// 提交表单
const submitForm = async () => {
  if (loading.value) return
  
  try {
    await passwordForm.value.validate()
    
    loading.value = true
    
    // 调用设置密码接口，让请求拦截器处理授权
    const params = new URLSearchParams();
    params.append('password', form.password);
    request.post('/saas/auth/set-password', params).then(res => {
      loading.value = false
      if (res.code === 200 || res.code === 0) {
        // 设置密码成功后，清除首次登录标志
        localStorage.removeItem('isFirstLogin')
        ElMessage.success('密码设置成功')
        router.push('/')  // 跳转到系统首页
      } else {
        ElMessage.error(res.msg || '密码设置失败，请稍后重试')
      }
    }).catch(error => {
      loading.value = false
      
      // 提取详细错误信息
      let errorMsg = '设置密码失败，请稍后重试'
      if (error.message) {
        errorMsg = error.message
      } else if (error.msg) {
        errorMsg = error.msg
      } else if (error.response && error.response.data) {
        if (error.response.data.message) {
          errorMsg = error.response.data.message
        } else if (error.response.data.msg) {
          errorMsg = error.response.data.msg
        }
      }
      
      ElMessage.error(errorMsg)
    })
  } catch (error) {
    ElMessage.error(error.message || '设置密码失败，请重试')
  }
}
</script>

<style scoped>
.set-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.set-password-card {
  width: 500px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
}

.tip {
  color: #909399;
  font-size: 14px;
}

.submit-btn {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}
</style> 